<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function animate(obj, taget) {
            clearInterval(obj.timer)
            obj.timer = setInterval(function() {
                var step = 10
                step = taget > obj.offsetLeft ? step : -step
                if (obj.offsetLeft != taget) {

                    obj.style.left = obj.offsetLeft + step + 'px'
                } else {
                    clearInterval(obj.timer)
                }
            }, 30)
        }
    </script>
</body>

</html>